package com.iweb.dao;

import com.iweb.pojo.Hotel;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class HotelDaoImpl implements HotelDao {
    private Connection connection;

    public HotelDaoImpl() {
    }

    public HotelDaoImpl(Connection connection) {
        this.connection = connection;
    }

    @Override
    public List<Hotel> selectList(String hotel_name) throws Exception{
        List<Hotel> hotels=new ArrayList<>();

        String sql="select * from tb_hotel ";
        if(hotel_name!=null){
            sql+="where hotel_name like ?";

        }

        PreparedStatement pst=connection.prepareStatement(sql);
        if(hotel_name!=null){
            pst.setObject(1,"%"+hotel_name+"%");
        }
        ResultSet rs=pst.executeQuery();

        Hotel hotel=null;
        while (rs.next()){
            hotel=createHotel(rs);
            hotels.add(hotel);
        }
        System.out.println(hotels);
        return hotels;
    }

    private Hotel createHotel(ResultSet rs) throws Exception{
        Hotel hotel=new Hotel();
        hotel.setHotel_id(rs.getString(1));
        hotel.setHotel_name(rs.getString(2));
        hotel.setHotel_bImg(rs.getString(3));
        hotel.setHotel_sImg(rs.getString(4));
        hotel.setHotel_introduce(rs.getString(5));
        hotel.setHotel_local(rs.getString(6));
        hotel.setHotel_price(rs.getString(7));
        return hotel;
    }
}
